-- Comment.lua
-- Created by cheny3 Aug/29/2016
-- Peter的木桌

require "game/ui/form/dungeon/special/UICommentDesk"

-- Peter的木桌
function initComment(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            local uiCommentDesk = UICommentDesk.create(self.gridData);
            UIMgr.getCurrentScene():removeFormByName("UICommentDesk");
            UIMgr.getCurrentScene():addForm(uiCommentDesk);

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 闪光
    self.shiningAjustPos = cc.p(70, 100);
    shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);

    -- 未评论，海外平台/雷霆IOS显示
    if BuffDataM.query("has_comment", 1) ~= 0 or not canShowPeterDesk() then
        -- 只能出现一次，所以失效了
        self.icon:setVisible(false);
    end

    local url = MaintainBulletinM.getPackageCommentUrl();
    local function whenResponse(responseData)
        if not responseData then
            -- 连接不上数据
            print("连接不上评论所在地址： " .. url);

            local pos = self.gridData:getPos();
            local grid = DungeonM.getGridByPos(pos);
            grid.limitUrl = true;
        end
    end
    if url ~= "" then
        getRemoteHostData(url, whenResponse);
    end
end